package cn.xz.diy_service_register.service;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.xz.diy_service_register.ajaxRes.AjaxResult;
import cn.xz.diy_service_register.pojo.RegisterVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class UserService {

    @Value(value = "${spring.application.name}")
    private String serviceName;
    @Value(value = "${server.port}")
    private Integer port;
    @Scheduled(cron = "0/5 * * * * *")
    public void register(){
        String url = "http://127.0.0.1:10010/serviceRegister/register";

        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("port",port.toString());
        hashMap.put("serviceName",serviceName);

        hashMap.put("Content-Type", "application/json;charset=UTF-8");
        System.out.println("发送请求.....");
//        String body = HttpUtil.sendPost(url, hashMap);

        String body = HttpUtil.post(url, JSON.toJSON(hashMap).toString());
        System.out.println(body);
    }
    @Scheduled(cron = "0/6 * * * * *")
    public void getServiceList(){
        String url = "http://127.0.0.1:10010/serviceRegister/find";

        System.out.println("发送拉取服务列表请求.....");
//        String body = HttpUtil.sendPost(url, hashMap);

        String body = HttpUtil.post(url,"");
        AjaxResult ajaxResult = JSONObject.parseObject(body, AjaxResult.class);
        Object data =  ajaxResult.getData();
        Map mapObj = JSONObject.parseObject(data.toString(),Map.class);
        System.out.println("服务列表"+mapObj);
    }

}
